﻿using C_Sharp_API_Example.src.Advanced.CustomRender;
using HandyControl.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace MyMeeting.Common
{
  public   class CustomVideoBox:Panel
    {

        private BaseRender render_ = null;
        private BaseRender.CustomVideoBoxRenderType type_ = BaseRender.CustomVideoBoxRenderType.kSharpDX_BGRA;

        private object obj_ = new object();

        public BaseRender.CustomVideoBoxRenderType RenderType
        {
            get { return type_; }
            private set { }
        }

       
        protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
        {
            base.OnRenderSizeChanged(sizeInfo);
            return;
            lock (obj_)
            {
                if (render_ != null)
                {
                    if (render_.NeedInvoke())
                    {
                        this.Dispatcher.Invoke(new Action(() =>
                        {
                            render_.UpdateSize(sizeInfo);
                        }));
                    }
                    else
                    {
                        render_.UpdateSize(sizeInfo);
                    }
                }
            }
        }




        public void SetRenderType(BaseRender.CustomVideoBoxRenderType type)
        {
            if (type_ == type) return;

            type_ = type;

            lock (obj_)
            {
                // only create render when first frame came in case that we do not want draw background by ourself
                // coze we will set style with AllPaintingInWmPaint true...
                if (render_ != null)
                    this.ReInitRender();
            }
        }

        public void DestroyRender()
        {
                this.Dispatcher.Invoke(new Action(() => {
                    if (render_ != null)
                        render_.Dispose();
                    render_ = null;
                }));
        }

        private void ReInitRender()
        {

            if (render_ != null && render_.GetRenderType() == type_)
            {
                return;
            }

            if (render_ != null)
            {
                render_.Dispose();
                render_ = null;
            }

            BaseRender.DataType data_type = BaseRender.DataType.kBGRA;
            render_ = new SharpDXRender();
            //switch (type_)
            //{
            //    case BaseRender.CustomVideoBoxRenderType.kBufferedGraphics:
            //        //this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            //        //this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            //        //this.SetStyle(ControlStyles.UserPaint, false);
            //        //render_ = new BufferedGraphicsRender(this.);
            //        //data_type = BaseRender.DataType.kBGRA;
            //        break;
            //    case BaseRender.CustomVideoBoxRenderType.kSharpDX_BGRA:
            //        //this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            //        //this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            //        //this.SetStyle(ControlStyles.UserPaint, false);
            //        //render_ = new BaseRender();
            //        //data_type = BaseRender.DataType.kBGRA;
            //        break;
            //    case BaseRender.CustomVideoBoxRenderType.kSharpDX_YUV420:
            //        //this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            //        //this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            //        //this.SetStyle(ControlStyles.UserPaint, false);
            //        //render_ = new SharpDXRender();
            //        //data_type = BaseRender.DataType.kYUV420;
            //        break;
            //    default:
            //        break;
            //}

            if (render_ != null && render_.Initialize(this.GetHandle(), this.DesiredSize, data_type) != true)
            {
                render_.Dispose();
                render_ = null;
            }
        }

        public void DeliverFrame(Agora.Rtc.VideoFrame videoFrame)
        {
            lock (obj_)
            {
                if (render_ == null)
                {
                    this.Dispatcher.Invoke(new Action(() =>
                    {
                        ReInitRender();
                    }));
                }

                if (render_.NeedInvoke())
                {
                    this.Dispatcher.Invoke(new Action(() =>
                    {
                        render_.DeliverFrame(videoFrame);
                    }));
                }
                else
                {
                    render_.DeliverFrame(videoFrame);
                }
            }
        }
    }
}
